package org.jsola.hr.service.impl;

import com.alibaba.excel.util.CollectionUtils;
import org.apache.shiro.util.StringUtils;
import org.jsola.admin.entity.SiteDO;
import org.jsola.core.Page;
import org.jsola.hr.provider.IAdminProviderService;
import org.jsola.hr.provider.ILogProviderService;
import org.jsola.hr.service.IHrActionLogService;
import org.jsola.log.query.ActionLogQuery;
import org.jsola.log.vo.ActionLogListVO;
import org.jsola.log.vo.ServiceLogListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 请求日志
 *
 * @author june
 */
@Service("hrHrActionLogServiceImpl")
public class HrActionLogServiceImpl implements IHrActionLogService {


    @Autowired
    private ILogProviderService logProviderService;

    @Autowired
    private IAdminProviderService adminProviderService;

    @Override
    public Page<ActionLogListVO> selectPage(ActionLogQuery actionLogQuery, String siteId) {
        Map<String, SiteDO> siteMap = adminProviderService.selectSiteMap();
        if (StringUtils.hasLength(actionLogQuery.getSiteName())) {
            List<String> siteIds = siteMap.values().parallelStream().map(siteDO -> {
                if (siteDO.getName() != null && siteDO.getName().contains(actionLogQuery.getSiteName())) {
                    return siteDO.getId();
                }
                return null;
            }).filter(Objects::nonNull).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(siteIds)) {
                return new Page<>(actionLogQuery.getPageSize(), actionLogQuery.getPageNo());
            }
            actionLogQuery.setSiteIds(siteIds);
        }
        Page<ActionLogListVO> actionLogListVoPage = logProviderService.selectActionLogPage(actionLogQuery, siteId);
        List<ActionLogListVO> actionLogListVOList = actionLogListVoPage.getEntities();
        if (CollectionUtils.isEmpty(actionLogListVOList)) {
            return actionLogListVoPage;
        }
        actionLogListVOList.parallelStream().forEach(serviceLogListVO -> {
            //组装租户名
            SiteDO siteDO = siteMap.get(serviceLogListVO.getSiteId());
            if (siteDO != null) {
                serviceLogListVO.setSiteName(siteDO.getName());
            }
        });
        return actionLogListVoPage;
    }
}




